package com.jz.yanshe.ui;

import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jz.jd.rest.business.RestProxy;
import com.jz.jd.rest.exception.BusinessException;
import com.jz.jd.rest.response.AddressResponse;
import com.jz.jd.rest.response.BaseResponse;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import jz.com.feierman.R;
import com.jz.yanshe.base.BaseActivity;
import com.jz.yanshe.util.PreferencesHelper;

/**
 * Created by Administrator on 2018/3/8.
 */

public class MyAddressActivity extends BaseActivity {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;



    MyAdapter myAdapter = new MyAdapter();
    List<AddressResponse.Address> list = new ArrayList<>();

    int type;

    @Override
    protected void initViews() {
        recyclerView.setAdapter(myAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDraw(c, parent, state);
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.bottom = 10;
            }
        });
    }

    @Override
    protected void initData() {
//        getAllAddress();
        type = getIntent().getExtras().getInt("id");
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_my_address;
    }


    class MyAdapter extends RecyclerView.Adapter<MyAdapter.Holder> {


        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            Holder holder = new Holder(getLayoutInflater().inflate(R.layout.item_address, parent, false));
            return holder;
        }

        @Override
        public void onBindViewHolder(final Holder holder, final int position) {
            if(position == 0){
                if (type != 1) {
                    holder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent();
                            intent.putExtra("obj", list.get(position));
                            setResult(2, intent);
                            finish();
                        }
                    });
                }
                if (list.get(position).is_primary == 1) {
                    holder.check_address.setChecked(true);
                } else {
                    holder.check_address.setChecked(false);
                }
                holder.tv_user_phone.setText(list.get(position).phone);
                holder.tv_user_address.setText(list.get(position).address_all);
                holder.tv_user_name.setText(list.get(position).nick_name);
                holder.check_address.setVisibility(View.INVISIBLE);
                holder.btn_edit.setVisibility(View.INVISIBLE);
                holder.btn_del.setVisibility(View.INVISIBLE);
                holder.tv_address_type.setText("自提地址");
            }else{


            if (type != 1) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent();
                        intent.putExtra("obj", list.get(position));
                        setResult(1, intent);
                        finish();
                    }
                });
            }
            if (list.get(position).is_primary == 1) {
                holder.check_address.setChecked(true);
            } else {
                holder.check_address.setChecked(false);
            }
            holder.tv_user_phone.setText(list.get(position).phone);
            holder.tv_user_address.setText(list.get(position).address_all);
            holder.tv_user_name.setText(list.get(position).nick_name);
            holder.check_address.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (holder.check_address.isChecked()) {
                        for (int i = 0; i < list.size(); i++) {
                            list.get(i).is_primary = 2;
                        }
                        list.get(position).is_primary = 1;
                        setDefaultAddress(list.get(position).user_address_id);
                    }
                    notifyDataSetChanged();
                }
            });
            holder.btn_edit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("obj",list.get(position));
                    startActivity(AddAddressActivity.class,bundle);
                }
            });
            holder.btn_del.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    delAddress(list.get(position).user_address_id);
                }
            });
            }
        }


        @Override
        public int getItemCount() {
            return list.size();
        }

        class Holder extends RecyclerView.ViewHolder {
            TextView tv_user_phone, tv_user_address, tv_user_name,tv_address_type;
            CheckBox check_address;
            LinearLayout btn_edit,btn_del;

            public Holder(View itemView) {
                super(itemView);
                check_address = itemView.findViewById(R.id.check_address);
                tv_user_phone = itemView.findViewById(R.id.tv_user_phone);
                tv_user_address = itemView.findViewById(R.id.tv_user_address);
                tv_user_name = itemView.findViewById(R.id.tv_user_name);
                btn_edit = itemView.findViewById(R.id.btn_edit);
                btn_del = itemView.findViewById(R.id.btn_del);
                tv_address_type = itemView.findViewById(R.id.tv_address_type);

            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        getAllAddress();
    }

    private void getAllAddress() {
        RestProxy.getInstance().getAllAddress(PreferencesHelper.getInstance().getString(MyAddressActivity.this,"token"), new Observer<AddressResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(AddressResponse addressResponse) {
                list.clear();
                AddressResponse.Address address = addressResponse.new Address();
                address.phone = "18202107094";
                address.address_all="上海市闵行区新龙路399弄1号宝龙城T4-603";
                address.nick_name = "成雯";
                list.add(address);
                list.addAll(addressResponse.listM);
                myAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable e) {
                if (e instanceof BusinessException) {
                toLogin(((BusinessException)e).errorCode);}
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @OnClick(R.id.btn_back)
    void btn_back() {
        finish();
    }


    private void setDefaultAddress(String user_address_id){
        RestProxy.getInstance().setDefaultAddress(PreferencesHelper.getInstance().getString(MyAddressActivity.this,"token"), user_address_id, new Observer<BaseResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseResponse baseResponse) {

            }

            @Override
            public void onError(Throwable e) {

                if (e instanceof BusinessException) {
                toLogin(((BusinessException)e).errorCode);
            }}

            @Override
            public void onComplete() {

            }
        });
    }

    @OnClick(R.id.btn_add)
    void btn_add(){
        startActivity(AddAddressActivity.class);
    }

    private void delAddress(String user_address_id){
        RestProxy.getInstance().delAddress(PreferencesHelper.getInstance().getString(MyAddressActivity.this,"token")
                , user_address_id, new Observer<BaseResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                Toast.makeText(MyAddressActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                getAllAddress();
            }

            @Override
            public void onError(Throwable e) {

                if (e instanceof BusinessException) {
                toLogin(((BusinessException)e).errorCode);
            }}

            @Override
            public void onComplete() {

            }
        });
    }




}
